package client

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/client"
	"likeadmin/core"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var SourceGroup = core.Group("/client", newSourceHandler, regSource, middleware.TokenAuth())

func newSourceHandler(srv client.ISourceService) *sourceHandler {
	return &sourceHandler{srv: srv}
}

func regSource(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *sourceHandler) {
		rg.GET("/source/list", handle.list)
		rg.GET("/source/detail", handle.detail)
		rg.GET("/source/ad_delivery", handle.adDelivery)
		rg.POST("/source/add", handle.add)
		rg.POST("/source/edit", handle.edit)
		rg.POST("/source/del", handle.del)
	})
}

type sourceHandler struct {
	srv client.ISourceService
}

// list source列表
func (hd sourceHandler) list(c *gin.Context) {
	var page request.PageReq
	var listReq req.SourceListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := hd.srv.List(page, listReq)
	response.CheckAndRespWithData(c, res, err)
}

// detail source详情
func (hd sourceHandler) detail(c *gin.Context) {
	var detailReq req.SourceDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := hd.srv.Detail(detailReq.SourceId)
	response.CheckAndRespWithData(c, res, err)
}

// add source新增
func (hd sourceHandler) add(c *gin.Context) {
	var addReq req.SourceAddReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &addReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Add(c, addReq))
}

// edit source编辑
func (hd sourceHandler) edit(c *gin.Context) {
	var editReq req.SourceEditReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Edit(editReq))
}

// del source删除
func (hd sourceHandler) del(c *gin.Context) {
	var delReq req.SourceDelReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &delReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Del(delReq.SourceId))
}

// adDelivery
func (hd sourceHandler) adDelivery(c *gin.Context) {
	var delReq req.SourceDelReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &delReq)) {
		return
	}
	res, err := hd.srv.AdDelivery(delReq.SourceId)
	response.CheckAndRespWithData(c, res, err)
}
